var app = getApp()
const date = new Date();
const years = [];
const months = [];
const days = [];
const hours = [];
const minutes = [];
var app = getApp()
//获取年
for (let i = 1900; i <= date.getFullYear() + 5; i++) {
  years.push("" + i);
}
//获取月份
for (let i = 1; i <= 12; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  months.push("" + i);
}
//获取日期
for (let i = 1; i <= 31; i++) {
  if (i < 10) {
    i = "0" + i;
  }
  days.push("" + i);
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    serchList: [],
    title: '',
    mdid: "",
    sex: '男',
    items: [{
        name: '男',
        value: '男',
      },
      {
        name: '女',
        value: '女',
      }
    ],
    // 时间选择器
    time: '',
    multiArray: [years, months, days],
    multiIndex: [0, 9, 16, 10, 17],
    indexx: null,
    val: {},
    use_title: '',
    // 用法
    uselist: {
      use_type: ["外敷", "口服"],
      use_dose: ["5~10g", "15~20g", "25~30g", ],
      use_num: ["一日四次", "一日三次", "一日两次", "一日一次"]
    },
    typeindex: "",
    doseindex: "",
    numindex: "",
    drugsArr: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var openid = options.openid
    var zid = options.zid
    var pid = options.pid
    wx.setNavigationBarTitle({
      title: options.title
    })
    that.setData({
      openid: openid,
      zid: zid,
      pid: pid
    })
    //查询所有处方
    app.util.request({
      'url': 'entry/wxapp/zhuanjia.chufangdesc',
      data: {
        pid: pid
      },
      success: function (res) {
        console.log(res)
        that.setData({
          detailcf: res.data
        })
      }
    })


  },
  bindinput(e) {
    var that = this
    that.setData({
      use_title: e.detail.value
    })
    if (that.data.use_title) {

    }
  },
  // 药品用法
  bindtypeChange(e) {
    console.log("药品用法", e)
    var that = this
    that.setData({
      typeindex: e.detail.value
    })

  },
  // 药品单次用量
  binddoseChange(e) {
    console.log("药品单次用量", e)
    var that = this
    that.setData({
      doseindex: e.detail.value
    })

  },
  // 用药次数
  bindnumChange(e) {
    console.log("药品用药次数", e)
    var that = this
    that.setData({
      numindex: e.detail.value
    })
  },
  clean() {
    this.setData({
      use_title: '',
      typeindex: '',
      doseindex: '',
      numindex: '',
    })
  },
  addsubmit(e) {
    console.log(e)
    var that = this
    var use_title = that.data.use_title
    var uselist = that.data.uselist
    var typeindex = that.data.typeindex
    var doseindex = that.data.doseindex
    var numindex = that.data.numindex
    var drugsArr = that.data.drugsArr
    if (use_title == "") {
      wx.showToast({
        title: '请输入药品名称',
        icon: "none"
      })
    } else if (typeindex == '') {
      wx.showToast({
        title: '请选择该药品的用法',
        icon: "none"
      })
    } else if (doseindex == '') {
      wx.showToast({
        title: '请选择该药品的单次用量',
        icon: "none"
      })
    } else if (numindex == '') {
      wx.showToast({
        title: '请选择该药品的用药次数',
        icon: "none"
      })
    } else {
      var drugs = {
        use_title: use_title,
        typeindex: typeindex,
        doseindex: doseindex,
        numindex: numindex,
        use_type: uselist.use_type[typeindex],
        use_dose: uselist.use_dose[doseindex],
        use_num: uselist.use_num[numindex],
      }
      console.log("药品", drugs)
      drugsArr.push(drugs)
      that.setData({
        drugsArr: drugsArr,
        use_title: '',
        typeindex: '',
        doseindex: '',
        numindex: '',
      })

    }
  },
  bindArrtypeChange(e) {
    console.log("23333e", e)
    var that = this
    var drugsArr = that.data.drugsArr
    var uselist = that.data.uselist
    var index = e.currentTarget.dataset.index
    for (var i = 0; i < drugsArr.length; i++) {
      if (index == i) {
        drugsArr[index].typeindex = e.detail.value
        drugsArr[index].use_type = uselist.use_type[e.detail.value]
        that.setData({
          drugsArr: drugsArr
        })
      }
    }
  },
  bindArrdoseChange(e) {
    var that = this
    var drugsArr = that.data.drugsArr
    var uselist = that.data.uselist
    var index = e.currentTarget.dataset.index
    for (var i = 0; i < drugsArr.length; i++) {
      if (index == i) {
        drugsArr[index].doseindex = e.detail.value
        drugsArr[index].use_dose = uselist.use_dose[e.detail.value]
        that.setData({
          drugsArr: drugsArr
        })
      }
    }
  },
  bindArrnumChange(e) {
    var that = this
    var drugsArr = that.data.drugsArr
    var uselist = that.data.uselist
    var index = e.currentTarget.dataset.index
    for (var i = 0; i < drugsArr.length; i++) {
      if (index == i) {
        drugsArr[index].numindex = e.detail.value
        drugsArr[index].use_num = uselist.use_num[e.detail.value]
        that.setData({
          drugsArr: drugsArr
        })
      }
    }
  },
  delArr(e) {
    var that = this
    var index = e.currentTarget.dataset
    var drugsArr = that.data.drugsArr
    drugsArr.splice(index, 1)
    that.setData({
      drugsArr: drugsArr
    })
  },
  // 去开方
  submit(e) {
    var that = this
    var value = e.detail.value
    if (value.zhenduan == "") {
      wx.showToast({
        title: '请输入诊断建议',
        icon: 'none'
      })
    } else if (value.yongyao == "") {
      wx.showToast({
        title: '请输入用药建议',
        icon: 'none'
      })
    } else if (value.chufang == "") {
      wx.showToast({
        title: '请输入处方详情',
        icon: 'none'
      })
    } else if (that.data.use_title != "") {
      var drugsArr = that.data.drugsArr
      if (that.data.numindex == '' || that.data.doseindex == '' || that.data.numindex == '') {
        wx.showToast({
          title: '请完善药品补充',
          icon: 'none',
        })
      } else {
        var arr = {
          use_title: that.data.use_title,
          typeindex: that.data.typeindex,
          doseindex: that.data.doseindex,
          numindex: that.data.numindex,
          use_type: that.data.uselist.use_type[that.data.typeindex],
          use_dose: that.data.uselist.use_dose[that.data.doseindex],
          use_num: that.data.uselist.use_num[that.data.numindex],
        }
        drugsArr.push(arr)
        that.setData({
          drugsArr: drugsArr,
          use_title: '',
          typeindex: '',
          doseindex: '',
          numindex: '',
        })
        console.log("drugsArr1333", that.data.drugsArr)
        console.log("drugsArr1333", value)

      }
    } else {
      console.log("drugsArr2333", that.data.drugsArr)
      console.log("drugsArr2333", value)



      var zid = that.data.zid
      var openid = that.data.openid
      var cid = e.currentTarget.dataset.id
      var pid = that.data.pid

      var chufang = value.chufang
      var yongyao = value.yongyao
      var zhenduan = value.zhenduan
      var drugsArr = that.data.drugsArr

      wx.showModal({
        title: '提示',
        content: '是否确认选择开此方？',
        success(res) {
          if (res.confirm) {
            app.util.request({
              'url': 'entry/wxapp/zhuanjia.dayin',
              data: {
                id: cid,
                zid: zid,
                openid: openid,
                pid: pid,
                chufang: chufang,
                yongyao: yongyao,
                zhenduan: zhenduan,
                drugsArr: drugsArr
              },
              success: function (res) {
                console.log(res)
                //发送模板消息
                var orderarr = res.data
                app.util.request({
                  'url': 'entry/wxapp/user.msgtixing',
                  data: {
                    openid: openid,
                    orderarr: orderarr
                  },
                  success: function (res) {
                    console.log(res)
                  }
                })
                wx.showToast({
                  title: '开方成功',
                  icon: 'none',
                  duration: 2000
                })
                setTimeout(function () {
                  wx.navigateBack({
                    detail: 1
                  })
                }, 2000)
              }
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log('11')
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this
    console.log('22', that.data.title)

    if (that.data.title == "") {
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];
      prevPage.setData({
        memberval: ""
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  bindPickerChange2: function (e) {
    var that = this;
    var dex = e.detail.value;
    var kcid = that.data.arrkc[dex].id;
    that.setData({
      kcid: kcid,
      title: that.data.arrkc[dex].title,
    })

  },
  bindPickerChange3: function (e) {
    var that = this;
    var zid = that.data.zid
    var openid = that.data.openid
    var cid = e.currentTarget.dataset.id
    var pid = that.data.pid
    wx.showModal({
      title: '提示',
      content: '是否确认选择开此方？',
      success(res) {
        if (res.confirm) {
          app.util.request({
            'url': 'entry/wxapp/zhuanjia.dayin',
            data: {
              id: cid,
              zid: zid,
              openid: openid,
              pid: pid
            },
            success: function (res) {
              console.log(res)
              wx.showToast({
                title: '开方成功',
                icon: 'none',
                duration: 2000
              })
            }
          })
        }
      }
    })
  },
  radioChange: function (e) {
    console.log(e)
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var items = this.data.items;
    this.setData({
      sex: e.detail.value
    })
    var items = this.data.items;
    for (var i = 0; i < items.length; ++i) {
      items[i].checked = items[i].value == e.detail.value
    }
    console.log(items)
    this.setData({
      items: items
    });
  },
  radioChangeArr: function (e) {
    console.log(e)
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var list = this.data.list;
    this.setData({
      name: e.detail.value
    })
    var mdid = this.data.mdid
    for (var i = 0; i < list.length; ++i) {
      list[i].checked = list[i].name == e.detail.value
      if (list[i].name == e.detail.value) {
        mdid = mdid.concat(list[i].id);
      }
    }
    this.setData({
      list: list,
      mdid: mdid
    });
  },

  bindsubmit(e) {
    var that = this
    var val = e.detail.value
    val.mdid = that.data.mdid
    val.kcid = that.data.kcid
    val.name = that.data.name
    val.tid = that.data.tid
    val.yytime = that.data.yytime
    val.detailtime = that.data.detailtime
    val.year = that.data.year
    val.useropenid = that.data.useropenid
    var title = val.title
    var tel = val.tel
    var kcname = val.kcname
    var sex = that.data.sex
    var ddtime = val.ddtime
    var shijian = val.shijian
    var age = val.age

    if (title == '') {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none',
      })
      return false
    }
    if (age == '') {
      wx.showToast({
        title: '请填写年龄',
        icon: 'none',
      })
      return false
    }
    if (tel == '') {
      wx.showToast({
        title: '请填写联系方式',
        icon: 'none',
      })
      return false
    }
    app.util.request({
      'url': 'entry/wxapp/addtcyuyue',
      data: {
        val
      },
      success: function (res) {
        console.log(res)
        val.yyid = res.data
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        wx.setStorageSync('detail', val)
        wx.setStorageSync('yyid', res.data)
        prevPage.setData({
          memberval: val
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        })
      }
    })

    that.setData({
      title: title
    })
  },
  bindMultiPickerChange: function (e) {
    console.log(e)
    console.log('picker发送选择改变，携带下标为', e.detail.value)

    this.setData({
      multiIndex: e.detail.value
    })
    const index = this.data.multiIndex;
    const year = this.data.multiArray[0][index[0]];
    const month = this.data.multiArray[1][index[1]];
    const day = this.data.multiArray[2][index[2]];
    this.setData({
      time: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      shijian: e.detail.value
    })
  },
  serch: function (e) {
    var serch_text = e.detail.value
    var listarr = this.data.list
    //console.log(listarr)
    var arr = []
    for (var i = 0; i < listarr.length; i++) {
      if (listarr[i].title.indexOf(serch_text) != -1) {
        arr.push(listarr[i])
      }
    }
    this.setData({
      serchList: arr
    })
    if (serch_text == '') {
      this.setData({
        serchList: []
      })
    }
  },
  close_btn: function (e) {
    this.setData({
      serch_text: ''
    })
  },
})